<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>0);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_agentmcp_newstype',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if(!$return['list']){
		$this->model('news')->upnewstype();
		$return['list']=pdo_getslice('jy_agentmcp_newstype',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级对话类型'),
			array('op'=>'edit','title'=>'添加一级对话类型'),
		),
		'list'=>array('data'=>'list','list'=>array(	
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'desc','title'=>'描述','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),				
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_agentmcp/newstype/upnewstype/','title'=>'同步类型'),
			array('type'=>'post','url'=>'jy_agentmcp/newstype/setstatus0/','title'=>'一键下架'),
			array('type'=>'post','url'=>'jy_agentmcp/newstype/setstatus1/','title'=>'一键启用'),
		),'action'=>array(
			array('type'=>'href','op'=>'list2','title'=>'二级对话类型','show'=>array('pid'=>0)),
		)),
	);
	$return['pids']=pdo_getall('jy_agentmcp_newstype',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='list2'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>0);
	if(!empty($_RQ['id'])){
		$params['pid']=intval($_RQ['id']);
		$return['params']['pid']=intval($_RQ['id']);
	}
	$parenttitle=pdo_getcolumn('jy_agentmcp_newstype',array('id'=>$params['pid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_agentmcp_newstype',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$basic=$this->plugin_setting('basic');
	foreach($return['list'] as &$item){
		$item['is_level3']=$basic['is_level3'];
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级对话类型'),
			array('op'=>'list2','title'=>$parenttitle.'对话类型列表'),
			array('op'=>'edit','title'=>'添加对话类型','query'=>'pid='.$params['pid']),
		),
		'list'=>array('data'=>'list','list'=>array(	
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'pid','title'=>'上级对话类型','type'=>'select','options'=>'pids'),
			array('name'=>'aimy','title'=>'角色','type'=>'string'),
			array('name'=>'aito','title'=>'对象','type'=>'string'),
			array('name'=>'aititle','title'=>'主题描述','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),				
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_agentmcp/newstype/setstatus0/','title'=>'一键下架'),
			array('type'=>'post','url'=>'jy_agentmcp/newstype/setstatus1/','title'=>'一键启用'),
		)),
	);
	$return['pids']=pdo_getall('jy_agentmcp_newstype',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_agentmcp_newstype',array('id'=>$id));
		$item['parenttitle']=pdo_getcolumn('jy_agentmcp_newstype',array('id'=>$item['pid']),'title');
	}else{
		$item=array();
		if($_RQ['pid']){
			$item['pid']=intval($_RQ['pid']);
			$item['parenttitle']=pdo_getcolumn('jy_agentmcp_newstype',array('id'=>$item['pid']),'title');
		}
	}
	$item['parenttitle']=$item['parenttitle']?:'无';
    $return['item']=$item;
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'岗位设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回一级对话类型列表'),
		),
		'edit'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'parenttitle','title'=>'上级对话类型','type'=>'copy'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'desc','title'=>'描述','type'=>'text'),
			array('name'=>'aimy','title'=>'角色','type'=>'string'),
			array('name'=>'aito','title'=>'对象','type'=>'string'),
			array('name'=>'aititle','title'=>'主题描述','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)
	);
	if($_RQ['pid']){
		array_unshift($htmls['edits'][0]['list'],array('name'=>'parenttitle','title'=>'上级对话类型','type'=>'string','read'=>true));
	}
	$return['pids']=pdo_getall('jy_agentmcp_newstype',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['xueli_type']=$this->xueli_type;
	$return['sex']=$this->sex;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('title','aimy','aito','aititle','desc','image','paixu'),$postdata);
	$id=intval($postdata['id']);
	if($id){		
		pdo_update('jy_agentmcp_newstype',$data,array('id'=>$id));
		exi('设置成功！','','/newstype/');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_agentmcp_newstype',$data);
		exi('添加成功！','','/newstype/');
	}
}
if($_RQ['op']=='upnewstype'){
	$this->model('news')->upnewstype();
	exi('同步成功！','','/newstype/');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的对话类型！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_agentmcp_newstype',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_agentmcp_newstype',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择对话类型','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_agentmcp_newstype',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('下架成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择对话类型','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_agentmcp_newstype',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('上架成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_agentmcp_newstype',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_agentmcp_newstype',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>